<?php

class Ead_PermissaoController extends App_Controller_Action
{
    
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction() {
        $obj = new Ead_Model_DbTable_Permissao();
        $result = $obj->fetchAll();
        $this->view->result = $this->paginacao($result);
    }
    
    public function addAction() {
        $this->_helper->viewRenderer('edit');
        
        $id = $this->_getParam('id', 0);
        
        $this->view->PER_ID = $id;
        
    }        
    
    public function editAction() {
        
        $id = $this->_getParam('id', 0);
        
        $form = new Ead_Form_Permissao(null, $id);
        $this->view->form = $form;
        
        $this->view->PER_ID = $id;
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            $arr_ck = $formData['MOD_ID'];
            
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('PER_ID');
                $data = array(
                    'PER_ID'     => $form->getValue('PER_ID'),
                    'PER_NOME'   => $form->getValue('PER_NOME'),	
                );
                $obj = new Ead_Model_DbTable_Permissao();
                $permisso = new Ead_Model_DbTable_PermissaoHasModulo();
                
                if ($id == 0){
                    $obj->insert($data);
                }
                else{
                    $obj->update($data, 'PER_ID = ' . $id);
                    $permisso->delete('PER_ID = ' . $id);
                }
                
                $permisso = new Ead_Model_DbTable_PermissaoHasModulo();
                $permisso->delete('PER_ID = '.(int)$id);
                foreach($arr_ck as $ck){
                    $data_permissao = array(
                        'PER_ID' => $form->getValue('PER_ID'),
                        'MOD_ID' => $ck,
                        'MOD_ADD' => isset($formData['MOD_ADD'][$ck]) ? $formData['MOD_ADD'][$ck]:0,
                        'MOD_EDT' => isset($formData['MOD_EDT'][$ck]) ? $formData['MOD_EDT'][$ck]:0,
                        'MOD_DEL' => isset($formData['MOD_DEL'][$ck]) ? $formData['MOD_DEL'][$ck]:0,
                    );
                    $permisso->insert($data_permissao);
                }
               $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $obj = new Ead_Model_DbTable_Permissao();
                $form->populate($obj->fetchRow('PER_ID = ' . $id)->toArray());
                $this->view->obj = $obj->fetchRow('PER_ID = ' . $id)->toArray();
            }
        }
    }

    public function deleteAction() {
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $obj = new Ead_Model_DbTable_Permissao();
            $obj->delete('PER_ID = '.(int)$id);
            
            $permisso = new Ead_Model_DbTable_PermissaoHasModulo();
            $permisso->delete('PER_ID = '.(int)$id);
            
            $this->_helper->redirector('index');
        } else {
            $id = $this->_getParam('id', 0);
            $obj = new Ead_Model_DbTable_Permissao();
            $form->populate($obj->fetchRow('PER_ID = ' . $id)->toArray());
        }
    }

   



}